import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 036. 后缀表达式
 * @author: WXY
 * @create: 2022-12-25 19:44
 * @Version 1.0
 **/

public class Num_offer2_036_evalRPN {
    public static int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String s : tokens) {
            if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
                int num1 = stack.pop();
                int num2 = stack.pop();
                switch (s) {
                    case "+":
                        stack.push(num2 + num1);
                        break;
                    case "-":
                        stack.push(num2 - num1);
                        break;
                    case "*":
                        stack.push(num2 * num1);
                        break;
                    case "/":
                        stack.push(num2 / num1);
                        break;
                }
            } else {
                stack.push(Integer.parseInt(s));
            }
        }
        return stack.pop();
    }

    public static void main(String[] args) {
        String[] str = new String[]{"4","13","5","/","+"};
        int i = evalRPN(str);
        System.out.println(i);
    }
}
